/*
do C:\Xu_Lin\Paper\Lee_Li\2011\Stata\weight1.do

 */
clear
set more off
log using C:\Xu_Lin\Paper\Lee_Li\2011\Stata\weight1.log,replace
#delimit ;

set mem 900m;

use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables,clear;

rename aid fid;
keep fid scid grade gpa 
; 

destring , replace;
sort fid;

save C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_fid,replace;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_1,clear;
keep aid fid1 fid2 fid3 fid4 fid5 fid6 fid7 fid8 fid9 fid10;
reshape long fid, i(aid) j(friend);

sort fid;
sum;

save C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\frd_long,replace;

merge fid using C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_fid;
sort aid;

tab _merge;

drop if _merge==1;
drop _merge;
save C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_friends,replace;

use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_1, clear;
keep aid scid grade;
rename scid scid_own;
rename grade grade_own;
sort aid;
save C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_scid, replace; 
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_friends, clear;

merge aid using C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_scid;
tab _merge;

replace fid=. if scid_own~=scid;
replace fid=. if grade_own~=grade;
drop if fid==.;

tab _merge;

drop _merge;

save C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\id_friends, replace;

use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_scid, clear;

sort aid;
save C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_aid, replace;

use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\id_friends,clear;
sort aid;
merge aid using C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_aid;
tab _merge;
replace fid=aid if _merge==2;
gen no_frd=.;
replace no_frd=1 if _merge==2;
drop _merge;

sort aid;

keep aid fid scid_own grade_own no_frd;

egen ggroup = group(scid_own grade_own);

keep aid fid ggroup no_frd;
sort aid;

save C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\id_friends, replace; 

set more on;
#delimit cr

log close
clear



